//
//  CiHuiHeaderView.m
//  Learn
//
//  Created by apple on 2024/4/8.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "CiHuiHeaderView.h"
@interface CiHuiHeaderView()
/* <#描述#> */
@property (nonatomic, strong) UIButton    * selectBtn;

@property (nonatomic, assign) NSInteger            selectIndex;       //

@end

@implementation CiHuiHeaderView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        self.selectIndex = 0;
    }
    return self;
}
- (void)setDataArray:(NSMutableArray *)dataArray {
    NSDictionary *dict = @{@"versionName":@"全部"};
    _dataArray = [NSMutableArray array];
    [_dataArray addObject:dict];
    [_dataArray addObjectsFromArray:dataArray];
    for (NSDictionary *dic in _dataArray) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSInteger index = [_dataArray indexOfObject:dic];
        btn.tag = 100 + index;
        CGFloat btnw = SCREEN_W/4 - 38/4;
        NSInteger row = index/4;
        btn.frame = CGRectMake(10 + (index%4)*(btnw + 6), 10 + 38*row, btnw, 28);
        
        btn.backgroundColor = COLOR_APP_BACKGROUND;
        [btn setTitle:dic[@"versionName"] forState:UIControlStateNormal];
        [btn setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        [btn setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];

        btn.titleLabel.font = PingFangMediumFont(12);
        if (index == self.selectIndex) {
            self.selectBtn = btn;
            self.selectBtn.selected = YES;
            self.selectBtn.backgroundColor = COLOR_TOPIC_SELECT;
        }
        
        [btn js_setRoundedCorners:UIRectCornerAllCorners radii:14];
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
}
- (void)btnClicked:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    self.selectBtn.backgroundColor = COLOR_APP_BACKGROUND;
    self.selectBtn.selected = NO;
    self.selectBtn = sender;
    self.selectBtn.backgroundColor = COLOR_TOPIC_SELECT;
    self.selectBtn.selected = YES;
    self.selectIndex = sender.tag - 100;
    if (self.selectBlock) {
        self.selectBlock(self.selectIndex);
    }
}


@end
